//logs.js
var app = getApp();
Page({
  data: {
    logs: [],
    tabId:0,
    showMore:0,
    userlist:[],
    userPage:1,
    thumb_up_ed:0,
    user_info:{},
    imgList:[],
    dayslimit:0,//可加入时间
    recordsTotal:0,
    commentLastpage:0,
    topicLastpage: 0,
    loading: 0,
    limitTopic:0,
    topicType:0,
    comment_page:1,//评论页数
    thumbs:[],//点赞列表 
    topicPage: 1,
    topicData:[],
    data:[],
    to_user_id:0,
    listData: [],   
    popup:0,
    starttime:'',
    endtime:'',
    remainTypeArray: ['日期', '周'],
    weekList: [],
    weekListStr: [],
    remainType: 3,
    noti: 0,
    dayIndex:0,
    countArray:[],
    countArray1: [1, 2, 3, 4],
    countArray2: [ 3, 5, 10, 15,20,30]
  },
  onLoad: function (options) {
    //问题详情
     var that = this;
     console.log(options);
     var user_info = wx.getStorageSync('user_info');// 83589 83586 83591 83651
     that.setData({ id: options.id ? options.id : 83640, user_info: user_info});
     if (options.topic_type>0){
       that.setData({ setTopicType:1, topicType: options.topic_type });
       if (options.topic_type==4){
         that.getUserlist();
       }
       
     }
    
    // that.setData({ topicType: 4 });
    wx.showShareMenu({
      withShareTicket: true
    })
    this.init();
  },
  setDayslimit(){
    console.log(this.data.data.startdate);
    var start = -this.DateMinus(this.data.data.content_data.startdate);
    var end = -this.DateMinus(this.data.data.content_data.enddate);
  
    this.setData({ startDays: start, endDays: end});
    console.log(end);
  },
   DateMinus(sDate){ 
    　　var sdate = new Date(sDate.replace(/-/g, "/")); 
       var now = new Date(app.date().replace(/-/g, "/")); 
      //  console.log(sdate);
      //  console.log(now);
    　　var days = now.getTime() - sdate.getTime(); 
    　　var day = parseInt(days / (1000 * 60 * 60 * 24)); 
    　　return day; 
  },
  init:function(type=0){
    if(type==1){
      this.setData({
        commentLastpage:0,
        topicLastpage: 0,
        loading: 0,
        topicPage: 1,
        comment_page:1,
        topicData: [],
        listData: [], 
      })
    }
    app.setPageData("topic/detail", { reload:0});
    var that = this;
    //evens/detail 活动详情 
    app.api('topic/detail', {
      id: that.data.id
    }, function (res) {
      wx.stopPullDownRefresh();
      if (res.code != 200) {
        app.alert(res.msg);
        return false;
      }
      if (res.data.pics) {
        that.setData({ imgList: res.data.pics });
      }
      // if (res.data.pid2>0){
      //   that.setData({ topicType: 1 });
      // }else{
      //   that.setData({ topicType: 2});
      // }
      if (res.data.content_data.averageDay==7){
        that.setData({ countArray: that.data.countArray1})
      }else{
        that.setData({ countArray: that.data.countArray2 })
      }
      that.setData({ thumb_up_ed: Number(res.data.thumb_up_ed) + 1 });
      that.setData({ data: res.data });
      //提醒的设置
      var setting = res.data.setting ? res.data.setting:{};
      that.setData({
        noti:setting.noti ? setting.noti:0,
        time: setting.time,
        remainType:setting.remainType,
        weekListStr: setting.weekListStr ? setting.weekListStr:[],
        weekList: setting.weekList ? setting.weekList:[],
        date: setting.date,
      })
      if (that.data.data.content_data.setDate){
         that.setDayslimit();
      }
      //动态列表
      console.log("content_data", that.data.data.content_data.signIn );
      if (that.data.data.content_data.signIn == false && that.data.data.pid2 == 0) {
        if (that.data.setTopicType!=1){
          that.setData({ topicType: 3 });
        }
        that.getTopicList(type);
      }else if (that.data.data.pid2 == 0) {
        if (that.data.data.topic_join && that.data.data.topic_join.id){
          that.getTopicList(type);
          if (that.data.setTopicType != 1) {
           that.setData({ topicType: 2 });
          }
        }else{
          that.getUserlist();
          if (that.data.setTopicType != 1) {
            that.setData({ topicType: 4 });
          }
        }
       
      } else if (that.data.setTopicType != 1) {
        that.setData({ topicType:1});
      }
    });
    //events/thumbs 活动赞列表 
    console.log(this.data.topicType);
    if (this.data.topicType != 4 && this.data.topicType != 2){
        app.api('topic/thumbs', {
          topic_id: that.data.id,
          order: 'id desc',
          limit: 4,
        }, function (res) {
          if (res.code != 200) {
            app.alert(res.msg);
            return false;
          }
          that.setData({ thumbs: that.data.thumbs.concat(res.data.list) });
        });
        //events/comments 活动评论 
        this.getComments(type);
    }
  },



  //api evens/list 活动列表 
  getTopicList(reset = 0) {
    if (this.data.topicType == 4 ) {
      return;
    }
    var that = this;
    // var order_by = that.data.showOrder;
    //重新加载 
    if (reset == 1) {
      that.setData({ topicPage: 1, topicLastpage:0, topicData: [] });
    }
    if (this.data.topicLastpage==1 || this.data.loading ==1){
      return false;
    }
    // if (that.data.topicData.length==0 ){
    //   var td = this.data.data;
    //   td.title =  td.desc;
    //   that.data.topicData[0] = td;
    // }
    this.data.loading=1;
    var page = that.data.topicPage;
    //console.log(event_type);
    app.api("topic/list", {
      page: page,
      order:'id desc',
      limit:20,
      pid2: this.data.data.id,
    }, function (res) {
      that.data.loading=0;
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
     // that.data.recordsTotal = res.data.recordsTotal;
      //console.log(res.data);
      if (res.data.list.length==0){
        that.data.topicLastpage=1; 
      }
      page++;
      that.setData({ recordsTotal: Number(res.data.recordsTotal), topicPage: page, topicData: that.data.topicData.concat(res.data.list) });
      var limitTopic = res.data.recordsFiltered -that.data.topicData.length;
      that.setData({ limitTopic: limitTopic });
    })
  },


  getUserlist() {
    var that = this;
    if (this.data.userloading == 1 || this.data.userlastPage == 1 ) {
      return false;
    }
    this.data.userloading = 1;
    app.api('topicjoin/get', {
      fields: "SnsTopicJoin.user,SnsTopicJoin.id,SnsTopicJoin.addtime,SnsTopicJoin.status,SnsTopicJoin.days",
      topic_id: that.data.id,
      order: "SnsTopicJoin.lastaddtime desc",
      page: this.data.userPage,
      limit: 20,
      status:3,
    }, function (res) {
      that.data.userloading = 0;
      if (res.code != 200) {
        app.alert(res.msg);
        return false;
      }
      if(res.data.list.length==0){
        that.data.userlastPage=1;
      }
      that.data.userPage++;
      // console.log(that.data.lists);
      that.setData({ join_count: res.data.recordsTotal, userlist: that.data.userlist.concat(res.data.list) });
 
    });
  },

  getComments(reset = 0){
    var that =this;
    var comment_page = that.data.comment_page;
    app.api('comment/get', {
      pid: that.data.id,
      page: comment_page,
      type:'todolist',
    }, function (res) {
      //console.log(res);
      if (res.code != 200) {
        app.alert(res.msg);
        return false;
      }
      comment_page++;
      that.setData({ comment_page: comment_page, listData: that.data.listData.concat(res.data.list) });
    });
  },

  //点选 events/thumb_up
  thumb_up(){
    var that = this;
    //console.log(user_info);
    var user_info = this.data.user_info;
    var topicData = that.data.data;
    app.thumb_up(that.data.id,function(res){
      if (res.type == 'add') {
        var thumbs = [{
          user_id: user_info.user_id,
          nickname: user_info.nickname,
          avatarUrl: user_info.avatarUrl,
        }].concat(that.data.thumbs);
        that.setData({ thumb_up_ed: 2 });
        ++topicData.good_count;
      } else if (res.type == 'del') {
        var thumbs = that.data.thumbs;
        thumbs.splice(0, 1);
        that.setData({ thumb_up_ed: 1 });
        --topicData.good_count;
      } 
      that.setData({ thumbs: thumbs, data: topicData });
    }) 
  },
  
//来自页面内转发按钮
  onShareAppMessage: function (res) {
    var that = this;
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    var title = this.data.user_info.nickname+"邀你参与“"+ app.util.cutString(that.data.data.title, 20)+"“计划 ！";
    return {
      title: title,
      path: '/pages/topic/detail?id='+that.data.data.id,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  
  commentAct:function(e){
    var to_user_id = e.currentTarget.dataset.user_id;
    var itemListArray = [];
    itemListArray.push({ text: '回复', act: this.to_reply });
    if (to_user_id == this.data.user_info.user_id || to_user_id==this.data.data.user_id){
      itemListArray.push({ text: '删除', act: this.del_comment });
    }
    var itemList = [];
    //console.log(itemListArray);
    for (var i in itemListArray) {
      itemList.push(itemListArray[i].text);
    }
    wx.showActionSheet({
      itemList: itemList,
      success: function (res) {
        console.log(itemListArray[res.tapIndex]);
        itemListArray[res.tapIndex]['act'](e);
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },
//回答
  reply:function(){
    this.setData({'popup':1});
    this.setData({ 'to_comment_id': 0,'to_user_id':this.data.data.user_id});
    this.setData({'reply':{value:'',placeholder:'这里写下你的评论...'}})
  },
 //回复
  to_reply:function(e){
    console.log(e);
    var to_comment_id = e.currentTarget.dataset.comment_id;
    var to_nickname = e.currentTarget.dataset.nickname;
    var to_user_id = e.currentTarget.dataset.user_id;
    this.setData({'popup':1});
    this.setData({ 'to_comment_id': to_comment_id, 'to_user_id': to_user_id});
    
    this.setData({'reply':{value:'',placeholder:'@'+to_nickname}})
  },
 //删除
 del_comment:function(e){
  var that = this;
  var idx = e.currentTarget.dataset.key;
  var comment_id = e.currentTarget.dataset.comment_id;
  var topicData = that.data.data;
  wx.showModal({
  title: '提示',
  content: '确认删除吗？',
  success: function(res) {
        if (res.confirm) {
          //删除评论
          app.api("comment/del",{
            id:comment_id,
            method: 'POST'
            },function(res){
             //删了出错
              if (res.code!=200){
                  app.alert(res.msg);
              }else{
                  wx.showToast({
                    title: '删除成功',
                    icon: 'success',
                    duration: 2000
                  })
                  var listData=that.data.listData;
                  listData[idx]['none']=1;
                  --topicData.comment_count;
                  that.setData({ listData: listData, data: topicData});
              }      
          });   
        }
       }
  })
 },

  
  //取消
  btn_cancel:function(){
     this.setData({'popup':0});
  },
  //提交
  formSubmit:function(e){
    var formId = e.detail.formId;
    var user_info = app.globalData.userInfo;
    var postData=e.detail.value;
    var topicData=this.data.data;
    if(postData.content==""){
      app.alert('回复不能为空哦');
     return false; 
    }
    this.setData({'loading':1});
    this.setData({'disabled':1});
    var _this=this;
    //提交回答
    app.api("comment/add",{
      to_comment_id:this.data.to_comment_id,
      pid:this.data.id,
      formId: formId,
      to_user_id: this.data.to_user_id,
      content:postData.content,
      type:'todolist',
      method: 'POST',
    },function(res){
      _this.setData({'loading':0});
      _this.setData({'disabled':0});
      var title="";
      if (res.code==200){
          title="提交成功"
          _this.setData({'popup':0});
           var listData=_this.data.listData;
           var len=listData.length;
           res.data.delAct=1;
           listData[len] = res.data;
           topicData.comment_count++;
           _this.setData({ listData: listData, data: topicData});
      }else{
        wx.alert(res.msg);
      }
      wx.showToast({
        title: title,
        icon: 'success',
        duration: 2000
      })
    })
    console.log(e);
  },
   //浏览图片
  previewImage: function (e) {
    var current = e.target.dataset.src
    wx.previewImage({
      current: current,
      urls: this.data.data.pics
    })
  },
 
 apply1: function (e) {
   var name = e ? e.currentTarget.dataset.name :'edit';
   var pid2 = this.data.pid2 > 0 ? this.data.pid2 : this.data.id;
   var options={};
   
   if(name =="edit"){
     wx.navigateTo({
       url: '/pages/topic/issue?topic_id='+this.data.id,
     })
   }else{

     if (this.data.data.topic_join && this.data.data.topic_join.status==2){
       this.showjoin();
      //  wx.navigateTo({
      //    url: '/pages/topic/detail?id=' + this.data.id +"&topic_type=4",
      //  })
       return false;
     }
     if (this.data.data.content_data.signIn && (this.data.data.has_sign_today || this.data.startDays > 0 || this.data.data.can_sign!=1)){
       return false;
     }
     wx.navigateTo({
       url: '/pages/topic/issue?pid2=' + pid2,
     })
   }
 },
 topic_del:function(){
   var id = this.data.data.id;
   wx.showModal({
     title: '删除计划',
     content: '确认删除计划吗？',
     success: function (res) {
       if (res.confirm) {
         app.api("topic/del", { id: id }, function (res) {
           if (res.code != 200) {
             app.alert(res.msg);
             return false;
           }
           app.alert("操作成功");
           app.setPageData("index/index", { reload2: 1 });
           app.setPageData("group/index", { reload: 1 });
           app.setPageData("user/timeline", { reload: 1 });
           app.setPageData("user/timeline", { reload2: 1 });
           app.setPageData("topic/detail", { reload: 1 });
           wx.navigateBack({
             delta: 1
           })
         })
       }
     }
   })  
 },
 del:function(){
   //var topic_id = this.data.id;
   var id = this.data.data.topic_join.id;
   wx.showModal({
     title: '退出计划',
     content: '确认退出计划吗？',
     success: function (res) {
       if (res.confirm) {
         app.api("topicjoin/del", { id: id},function(res){
            if (res.code != 200) {
              app.alert(res.msg);
              return false;
            }
            app.alert("操作成功");
            app.setPageData("index/index", { reload2: 1 });
            app.setPageData("group/index", { reload: 1 });
            app.setPageData("user/timeline", { reload: 1 });
            app.setPageData("user/timeline", { reload2: 1 });
            app.setPageData("topic/detail", { reload: 1 });
            wx.navigateBack({
              delta: 1
            })
          })
       }}
   })   
 },
 thumb_list_up: function (e) {//列表点赞 
   var that = this;
   var index = e.currentTarget.dataset.id;
   var topic_id = e.currentTarget.dataset.topic_id;
   app.thumb_up(topic_id, function (res) {
     var topicData = that.data.topicData[index];
     if (res.type == "add") {
       topicData.good_count++;
       topicData.thumb_up_ed = 1;
     } else if (res.type == "del") {
       topicData.good_count--;
       topicData.thumb_up_ed = 2;
     }
     that.data.topicData[index] = topicData;
     that.setData({
       topicData: that.data.topicData
     });
   })
   // console.log(topicData);
 },
 //浏览图片
 previewListImage: function (e) {
   var current = e.target.dataset.src;
   var index = e.target.dataset.index;
   var pics = this.data.topicData[index].pics;
   wx.previewImage({
     current: current,
     urls: pics
   })
 },
 //滚到底部 
 scrolltolower: function (e) {
   this.getComments();
 },
 topicScrolltolower:function(e){
   this.getTopicList();
 },
 //下拉刷新
 onPullDownRefresh: function () {
   this.init(1);
 },
 onHide: function(){
   this.setData({ popup: 0, showShare:0});
  // this.setData({ showShare: 0 });
 },
 onShow: function () {
   var reload = app.getPageData("topic/detail", 'reload');
   if (reload == 1) {
     this.init(1);
   }
 },



 moreAct:function(){
   var that =this;
   var itemListArray=[];
   if(this.data.topicType==1){ //详情
     if (this.data.data.user_id == this.data.user_info.user_id){
       itemListArray.push({ text: '编辑', act: this.apply1 });
     }
     itemListArray.push({ text: '回到首页', act: function(){
       wx.switchTab({ url:'/pages/user/timeline'})
     }});
   }
   if (this.data.topicType == 2) { //打卡计划
     if (this.data.data.user_id == this.data.user_info.user_id) {
       itemListArray.push({ text: '编辑', act: this.apply1 });
     }
     if (this.data.data.topic_join.status <2) {
       itemListArray.push({ text: '提醒设置', act: this.setting });
     }
     if (this.data.data.topic_join.status == 2) {
       itemListArray.push({ text: '退出计划', act: this.del });
     }

     itemListArray.push({ text: '炫耀一下', act: this.make_image2 }) ;
     if (this.data.data.user_join==1){
       itemListArray.push({ text: '排行', act: this.rank }) ;
     }
   }
   if (this.data.topicType == 3) { //私人计划
     if (this.data.data.status <2) {
      itemListArray.push({ text: '提醒设置', act: this.setting });
     }
     if (this.data.data.user_id == this.data.user_info.user_id) {
       itemListArray.push({ text: '编辑', act: this.apply1 });
     }
     if (this.data.data.user_id == this.data.user_info.user_id) {
       itemListArray.push({ text: '删除', act: this.del });
     }
     if (this.data.data.status == 0) {
       itemListArray.push({ text: '开始计划', act: this.updateStatus });
     }
     if (this.data.data.status == 1) {
       itemListArray.push({ text: '完成计划', act: this.updateStatus });
     }
     itemListArray.push({ text: '分享', act: this.make_image });
   }

   if (this.data.topicType == 4) { //计划
     itemListArray.push({ text: '邀请好友', act: this.showShare });
     if (this.data.data.user_id == this.data.user_info.user_id){
       itemListArray.push({ text: '编辑', act: this.apply1 });
       itemListArray.push({ text: '删除', act: this.topic_del });
     }
   }

   var itemList = [];
   //console.log(itemListArray);
   for (var i in itemListArray){
     itemList.push(itemListArray[i].text);
   }
   wx.showActionSheet({
     itemList: itemList,
     success: function (res) {
       console.log(itemListArray[res.tapIndex]);
       itemListArray[res.tapIndex]['act']();
      //  if (res.tapIndex == 0) {
      //    that.apply1();
      //  } else if (res.tapIndex == 1) {
      //    that.del();
      //  } else if (res.tapIndex == 2){
      //    that.go_ahead();
      //  } 
     },
     fail: function (res) {
       console.log(res.errMsg)
     }
   })
 },

 updateStatus:function(){
   var that = this;
   if (that.data.payloading ==1){
     return;
   }
   that.data.payloading = 1;
   app.api("topicjoin/updateStatus", {
     id: this.data.data.topic_join.id
   }, function (res) {
     if (res.code != 200) {
       app.alert(res.msg);
       that.data.payloading = 0;
       return false;
     } 
     that.suc('操作成功');
   })
 },
 go_ahead:function(){
   wx.navigateTo({
     url: '/pages/pay/index?topic_id=' + this.data.id,
   })
 },
 make_image:function(){
   //生成海报
   wx.navigateTo({
     url: '/pages/draw/index?id=' + this.data.id,
   })
 },

 make_image2: function () {
   //生成海报
   wx.navigateTo({
     url: '/pages/draw/share?id=' + this.data.id,
   })
 },


 join_2: function () {
   var topic_id = this.data.id;
   var that = this;
   if (that.data.payloading == 1) {
     return false;
   }
   that.data.payloading = 1;
   app.api("topicjoin/join", {
     topic_id: topic_id
   }, function (res) {
     if (res.code != 200) {
       app.alert(res.msg);
       that.data.payloading = 0;
       return false;
     }
     that.suc();
     wx.navigateBack({
       delta: 1
     })
   })
 },

 join:function(e){
  //  if (!this.data.data.content_data.money>0){
  //    this.doJoin();
  //    return ;
  //  }
   this.data.formId = e.detail.formId;
   var that = this;
   var data = this.data.data.content_data;
   if(this.data.endtime==""){
     app.alert("请设置打卡时间");
     return false;
   }
   if (this.data.starttime == "") {
     app.alert("请设置打卡时间");
     return false;
   }
   if (this.data.dayIndex <0) {
     app.alert("请设置打卡时长");
     return false;
   }
   var title="今天起，做一个自律的人";
   var content =  "打卡时间段："+this.data.starttime + "〜" + this.data.endtime ;
   wx.showModal({
     title:title,
     confirmText:'确认参与',
     content: content,
     success: function (res) {
       if (res.confirm) {
         that.doJoin(e);
         console.log('用户点击确定')
       } else if (res.cancel) {
         console.log('用户点击取消')
       }
     }
   })
 },

 doJoin:function(){//加入计划
   var id = this.data.data.id;
   this.pay(id);
 },
 suc: function (tip ='加入成功'){
   app.alert(tip);
  this.init(1);
  app.setPageData("user/timeline", { reload2: 1 });
  
 },
 pay: function (topic_id) {
   var that = this;
   if (that.data.payloading == 1) {
     return false;
   }
   that.setData({ loading2: 1, disabled2:1});
   that.data.payloading = 1;
   app.api("pay/topic_join", {
     topic_id: topic_id,
     formId: this.data.formId,
     endtime: this.data.endtime,
     starttime: this.data.starttime,
     dayIndex: this.data.countArray[this.data.dayIndex]
   }, function (res) {
     if (res.code != 200) {
       app.alert(res.msg);
       that.data.payloading = 0;
       that.setData({ 'loading2': 0 });
       that.setData({ 'disabled2': 0 });
       return false;
     }
     var order_sn = res.data.order_sn;
     app.api("pay/topay", {
       order_sn: order_sn
     }, function (res) {
       if (res.code != 200) {
         app.alert(res.msg);
         that.data.payloading = 0;
         that.setData({ 'loading1': 0 });
         that.setData({ 'disabled2': 0 });
         return false;
       }
       if (res.data.over == 1) {
         that.suc();
         return;
       }
       var params = res.data;
       //params.total_fee = 0.1;
       params.success = function (res) {
         that.setData({ loading2: 0, disabled2: 0 });
         that.suc();
         wx.navigateBack({
           delta: 1
         })
       }
       params.fail = function (res) {
         console.log(res);
         if (res.errMsg == 'requestPayment:fail cancel') {
           //  wx.navigateBack({
           //    delta: 1
           //  })
          //  that.setData({ 'loading': 0 });
          //  that.setData({ 'disabled': 0 });
           that.setData({ loading2: 0, disabled2: 0 });
           return false;
         }
         app.alert(res.errMsg);
       }
       console.log(params);
       wx.requestPayment(params)

     })
   })
 },
 switch2Change: function (e) {
  //  var name = e.currentTarget.dataset.type;
  //  console.log(e);
  //  if (name == "noti") {
    
  //  } else if (name == "signIn") {
  //    this.setData({ signIn: e.detail.value });
  //  }
   this.setData({ noti: e.detail.value });
 },
 bindTimeChange: function (e) {
   var type = e.currentTarget.dataset.type;
   console.log('picker发送选择改变，携带值为', e.detail.value);
   if (type == "starttime"){
     this.setData({
       starttime: e.detail.value
     })
   } else if(type =='time'){
     this.setData({
       time: e.detail.value
     })
   }else{
     this.setData({
       endtime: e.detail.value
     })
   }
   
 },
 bindRemainTypeChange: function (e) {
   //console.log(e);
   var v = Number(e.detail.value);
   this.setData({ remainType: v });
   if (v == 1) {
     this.setData({ orderListActive: 1 });
   }
 },
 showWeek: function () {
   this.setData({ orderListActive: 1 });
 },
 chooseWeek: function (e) {
   var index = app.in_array(e.currentTarget.dataset.key, this.data.weekList)
   if (index === false) {
     this.data.weekList.push(e.currentTarget.dataset.key);
     this.data.weekListStr.push(e.currentTarget.dataset.text);
   } else {
     this.data.weekListStr.splice(index, 1);
     this.data.weekList.splice(index, 1);
   }
   this.setData({
     weekList: this.data.weekList,
     weekListStr: this.data.weekListStr
   });
   //console.log(this.data.weekListStr);
 }, 
 bodyclick() {
   console.log('bodyclick');
   if (this.data.orderListActive == 1) {
     this.setData({ orderListActive: 0 });
   }
 },
 setting(){
   this.setData({ popup:2});
 },
 formSubmit2(e){
   var formId = e.detail.formId;
   var that =this;
   if (this.data.loading == 1 || this.data.disabled==1){
     return false;
   }
   this.setData({ 'loading': 1 });
   this.setData({ 'disabled': 1 });
   var contentData={};
   //有设置提醒
   console.log(this.data);
   if (this.data.noti) {
     contentData.weekList = this.data.weekList;
     contentData.weekListStr = this.data.weekListStr;
     contentData.remainType = this.data.remainType;
     contentData.noti = this.data.noti;
     contentData.time=this.data.time;
     contentData.date = this.data.date;
   }
   console.log(contentData);
   var id = this.data.data.topic_join.id;
   if(!id>0){
     app.alert("id不存在");
     return false;
   }
   app.api("topic_noti/add", {
     id: id,
     formId: formId,
     topic_join_id:id,
     topic_id:this.data.id,
     noti: contentData.noti,
     remainType: contentData.remainType,

     setting: JSON.stringify(contentData),
     method: 'POST',
    }, function (res) {
     if (res.code != 200) {
       app.alert(res.msg);
       that.data.payloading = 0;
       that.setData({ 'loading': 0 });
       that.setData({ 'disabled': 0 });
       return false;
     }
     app.alert("保存成功");
     that.setData({ 'loading': 0, 'disabled': 0, popup:0 });


    })

 },
 //设置时间
 bindDateChange: function (e) {
   var v = e.detail.value;
   this.setData({ date: v });

 },
 showjoin:function(){
   if (this.data.data.topic_join && this.data.data.topic_join.sign_start_time){
     this.setData({ starttime: this.data.data.topic_join.sign_start_time });
   }
   if (this.data.data.topic_join && this.data.data.topic_join.sign_end_time) {
     this.setData({ endtime: this.data.data.topic_join.sign_end_time });
   }
   if (this.data.data.content_data.setDate){
     this.datedifference();
     var countArray = [];
     var idays = this.data.idays < this.data.endDays ? this.data.idays : this.data.endDays;
     var step = parseInt(idays / 3);
     countArray = [step, step * 2, idays];
     if (idays < 20) {
       countArray = [idays]
     }
     this.setData({ countArray: countArray });
   }
   this.setData({ popup: 3 });
   this.paymoney();
   //this.data.idays
 },

 paymoney:function(){
   var day = this.data.countArray[this.data.dayIndex];
   var paymoney = (Number(day)) * this.data.data.content_data.money;
   this.setData({paymoney: paymoney });
 },

 bindDayChange: function (e) {
   var v = Number(e.detail.value);
   this.setData({ dayIndex: v });
   this.paymoney();
 },
 showShare:function(){
   this.setData({ showShare:1});
 },
 hideShare:function(){
   this.setData({ showShare: 0 });
 },
 make_image2: function () {
   //生成海报
   wx.navigateTo({
     url: '/pages/draw/rank?id=' + this.data.id,
   })
 },
  showMore:function(){
    var showMore = !this.data.showMore;
    this.setData({ showMore: showMore})
  },
  rank:function(){
    wx.navigateTo({
      url: '/pages/topic/rank?id=' + this.data.id,
    })
  },
  // showappointment:function(){
  //   this.setData({ popup: 4 });
  //   this.datedifference();
    
  // },

  datedifference: function () {    //sDate1和sDate2是2006-12-  
    var sDate1 = this.data.data.content_data.startdate;
    var sDate2 = this.data.data.content_data.enddate;
    var dateSpan,
      tempDate,
      iDays;
    sDate1 = Date.parse(sDate1);
    sDate2 = Date.parse(sDate2);
    dateSpan = sDate2 - sDate1;
    dateSpan = Math.abs(dateSpan);
    iDays = Math.floor(dateSpan / (24 * 3600 * 1000));
    this.setData({ idays: iDays });
  },
  //预约加入
  appointment: function (e) {
    var formId = e.detail.formId;
    console.log(formId);
    console.log(e);
  },
     /**
 * 页面上拉触底事件的处理函数
 */
   onReachBottom: function () {
     this.getTopicList();
   },
   indexTab(e) {
     if (e) {
       var tabId = (e.currentTarget.dataset.id);
       this.setData({ tabId: tabId });
     }
     if (this.data.userload || this.data.topicload) {
        return;
     }
     if (this.data.tabId == 0) {
       //this.setData({ lists: this.data.list1 });
       //this.getRankWeek();
       this.data.topicload = 1;
     } else {
     //  this.setData({ lists: this.data.list2 });
       //this.getRank();
       this.getUserlist();
       this.data.userload=1;
     }
   },
   moreUserAct:function(e){
     var user_id = e.currentTarget.dataset.user_id;
     var itemListArray = [];
     itemListArray.push({ text: '查看主页', act: function(){
       wx.navigateTo({
         url: '/pages/user/profile?id=' + user_id,
       })
     } });

     itemListArray.push({
       text: '提醒Ta打卡', act: this.noti,
     });

     var itemList = [];
     for (var i in itemListArray) {
       itemList.push(itemListArray[i].text);
     }
     wx.showActionSheet({
       itemList: itemList,
       success: function (res) {
         console.log(itemListArray[res.tapIndex]);
         itemListArray[res.tapIndex]['act'](e);
       },
       fail: function (res) {
         console.log(res.errMsg)
       }
     })
   },
   noti:function(e){
     var user_id = e.currentTarget.dataset.user_id;
     var nickname = e.currentTarget.dataset.nickname;
     this.setData({ notiUser: { user_id: user_id, nickname: nickname}});
     console.log(user_id);
     this.setData({ popup: 4 });
   },
   sendNoti:function(e){
     console.log(e);
     if(this.data.loading3==1){
       return false;
     }
     var that = this;
     var formId = e.detail.formId;
     var user_info = app.globalData.userInfo;
     var postData = e.detail.value;
     this.setData({ loading3: 1, disabled3:1});
     app.api("user/notiUser", {
       to_user_id: this.data.notiUser.user_id,
       formId: formId,
       id:this.data.id,
       content: postData.notiText,
       method: 'POST',
     }, function (res) {
       if (res.code != 200) {
         app.alert(res.msg);
         that.data.payloading = 0;
         that.setData({ 'loading3': 0 });
         that.setData({ 'disabled3': 0 });
         return false;
       }
       app.alert("发送成功");
       that.setData({ 'loading3': 0, 'disabled3': 0, popup: 0 });

     })

   },
   bindplay:function(e){
    console.log(e);
    var videoContext = wx.createVideoContext('myVideo');
    videoContext.requestFullScreen();
   },
   bindended:function(){
     var videoContext = wx.createVideoContext('myVideo');
     videoContext.exitFullScreen();
   }
   

   
})
